package com.gasolinator.a.app.helper;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;
import com.gasolinator.a.R;
import com.gasolinator.a.app.GasolinatorStation;
import com.gasolinator.a.augmented_reality.ui.GasolinatorMarker;
import com.gasolinator.a.augmented_reality.ui.Marker;
import com.google.android.maps.GeoPoint;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: fjhidalgo
 * Date: 29/11/11
 * Time: 13:30
 * To change this template use File | Settings | File Templates.
 */
public class GeoPointHelper {


    private static final String TAG = "GeoPointHelper";



    public static Location getCurrentGeoPoint(Context context) {

        GeoPoint geoPoint = null;
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(false);
        criteria.setPowerRequirement(1);
        criteria.setAccuracy(1);
        String providerFine = locationManager.getBestProvider(criteria, true);

        Log.d(TAG, "USING location provider fine = " + providerFine);
        criteria.setAccuracy(2);
        String providerCoarse = locationManager.getBestProvider(criteria, true);
        Log.d(TAG, "USING location provider coarse = " + providerCoarse);
        boolean errorLocation = false;

        if ((providerFine == null) && (providerCoarse == null)) {
            Toast localToast = Toast.makeText(context, R.string.error_location, 1);
            localToast.setGravity(17, 0, 0);
            localToast.show();
            errorLocation = true;
        }
        Log.d(TAG, "USING errorLocation = " + errorLocation);

        Location location = null;
        if (!errorLocation) {
            String provider = (providerFine == null) ? providerCoarse : providerFine;
//            provider = LocationManager.GPS_PROVIDER;
            Log.d(TAG, "USING best provider = " + provider);
            location = locationManager.getLastKnownLocation(provider);

            Log.d(TAG, "USING location = " + location);
            Log.d(TAG, "USING geoPoint = " + geoPoint);
            Log.d(TAG, "USING |=== FINISH ===|");
        }
        return location;

    }

    public static Criteria getCriteriaForProviderFine() {
        Criteria criteria = new Criteria();
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(false);
        criteria.setPowerRequirement(1);
        criteria.setAccuracy(1);
        return criteria;
    }


    public static Criteria getCriteriaForProviderCoarse() {
        Criteria criteria = new Criteria();
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(false);
        criteria.setPowerRequirement(1);
        criteria.setAccuracy(2);
        return criteria;
    }

    public static void addProximityAlertToGasStations(Activity activity, LocationManager locationMgr, List<Marker> markers) {
        if (markers==null) return;
        for (Marker obj : markers) {
            GasolinatorStation station= ((GasolinatorMarker) obj).getStation();
            Intent intent = new Intent("com.gasolinator.a.app.REQUEST_DFGS");
            intent.putExtra("name", station.getName());
            intent.putExtra("id", station.getName());
            PendingIntent proximityIntent = PendingIntent.getActivity(activity, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
            locationMgr.addProximityAlert(station.getLatitude(),station.getLongitude(),10,-1,proximityIntent);
        }
    }

    public static GeoPoint getCheapest(List<Marker> markers, String combustible) {
        int cheapestIndex = getCheapestIndex(markers,combustible);
        if (cheapestIndex==-1) return null;
        Marker gasolinatorCheaperMarker = markers.get(cheapestIndex);
        final GasolinatorStation gasolinatorStation = ((GasolinatorMarker) gasolinatorCheaperMarker).getStation();
        return gasolinatorStation2GeoPoint(gasolinatorStation);
    }

    public static GeoPoint gasolinatorStation2GeoPoint(GasolinatorStation gasolinatorStation)
    {
        if (gasolinatorStation == null) {
            return null;
        }
        int i = (int) (gasolinatorStation.getLatitude() * 1000000.0D);
        int j = (int) (gasolinatorStation.getLongitude() * 1000000.0D);
        return new GeoPoint(i, j);
    }

    public static int getCheapestIndex(List<Marker> markers, String combustible)
    {
        int cheapestIndex=-1;
        double priceTemp = 999999999.0D;
        int size = markers.size();

        for (int x=0 ; x < size ; x++) {
            GasolinatorStation gasolinatorStation = ((GasolinatorMarker)markers.get(x)).getStation();
            double price = gasolinatorStation.getPrice(combustible);
            if ((price != -1.0D) && (price < 999999999.0D)) {
                priceTemp = (priceTemp>price) ? price : priceTemp;
                cheapestIndex = (priceTemp==price) ? x : cheapestIndex;
            }
        }
        return cheapestIndex;
    }

    public static GeoPoint location2GeoPoint(Location currentLocation) {
        if (currentLocation == null) {
            return null;
        }
        int i = (int) (currentLocation.getLatitude() * 1000000.0D);
        int j = (int) (currentLocation.getLongitude() * 1000000.0D);
        return new GeoPoint(i, j);
    }
}
